﻿using LibraryManagementSystem;
using System;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Xml.Linq;

namespace LibraryManagementSystem
{
    public partial class RegisterForm  : Form
{
    public RegisterForm()
        {
            InitializeComponent();
            txtPassword.PasswordChar = '*';
            txtConfirmPassword.PasswordChar = '*';
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text.Trim();
            string password = txtPassword.Text;
            string confirmPassword = txtConfirmPassword.Text;
            string name = txtName.Text.Trim();
            string email = txtEmail.Text.Trim();
            string phone = txtPhone.Text.Trim();

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) ||
                string.IsNullOrEmpty(confirmPassword) || string.IsNullOrEmpty(name))
            {
                MessageBox.Show("请填写必填字段", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (password.Length < 6)
            {
                MessageBox.Show("密码长度至少6位", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (password != confirmPassword)
            {
                MessageBox.Show("两次输入的密码不一致", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (IsUsernameExists(username))
            {
                MessageBox.Show("用户名已存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (CreateUser(username, password, name, email, phone, false))
            {
                MessageBox.Show("注册成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("注册失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool IsUsernameExists(string username)
        {
            using (var connection = DatabaseConfig.GetConnection())
            {
                connection.Open();

                string query = "SELECT COUNT(*) FROM Users WHERE Username = @username";
                var command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@username", username);

                int count = Convert.ToInt32(command.ExecuteScalar());
                return count > 0;
            }
        }

        private bool CreateUser(string username, string password, string name, string email, string phone, bool isAdmin)
        {
            using (var connection = DatabaseConfig.GetConnection())
            {
                connection.Open();

                string salt = SecurityHelper.GenerateSalt();
                string passwordHash = SecurityHelper.HashPasswordWithSalt(password, salt);

                string query = @"
            INSERT INTO Users (Username, PasswordHash, Salt, Name, Email, Phone, IsAdmin)
            VALUES (@username, @passwordHash, @salt, @name, @email, @phone, @isAdmin)";

                var command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@username", username);
                command.Parameters.AddWithValue("@passwordHash", passwordHash);
                command.Parameters.AddWithValue("@salt", salt);
                command.Parameters.AddWithValue("@name", name);
                command.Parameters.AddWithValue("@email", string.IsNullOrEmpty(email) ? DBNull.Value : (object)email);
                command.Parameters.AddWithValue("@phone", string.IsNullOrEmpty(phone) ? DBNull.Value : (object)phone);
                command.Parameters.AddWithValue("@isAdmin", isAdmin);

                int result = command.ExecuteNonQuery();
                return result > 0;
            }
        }

        private void chkShowPassword_CheckedChanged(object sender, EventArgs e)
        {
            txtPassword.PasswordChar = chkShowPassword.Checked ? '\0' : '*';
            txtConfirmPassword.PasswordChar = chkShowPassword.Checked ? '\0' : '*';
        }

        

        
    }
}